﻿using Prism.Commands;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace WpfApp1
{
    public class MainViewModel
    {
        public ObservableCollection<Test> Tests { get; set; }
        public bool[] Sources = new bool[100];
        public MainViewModel()
        {
            Tests = new ObservableCollection<Test>();
            Tests.Add(new Test() { Id = 6 });
            Tests.Add(new Test() { Id = 9 });
            Test.Value = 99;
            //EventMgrLib.EventMgr.Ins.GetEvent<TestDataPubsub>().Subscribe(async (p) =>await Data(p));
            Sources[5] = true;
            Sources[10] = true;
            Sources[15] = true;
        }
        public DelegateCommand OpCmd => new DelegateCommand(Op);

        private void Op()
        {

        }
        private void U20()
        {
            string fileName = "d:\\" + MakeFileName(46);
            StringBuilder sb = new StringBuilder();
            sb.Append("[Index]\n");
            sb.Append("Format=MaintenanceReport\n");
            sb.Append("Version=1.0000\n");
            sb.Append("Machine=NPM\n");
            sb.Append($"Date=\"{DateTime.Now.ToString("yyyy/MM/dd,HH:mm:ss")}\"\n");
            sb.Append("AuthorType=Machine\n");
            sb.Append("Author=Machine\n");
            sb.Append("Diff=\n");
            sb.Append("MJSID=\"\"\n");
            sb.Append("Comment=\"\"\n");

            sb.Append("\n");

            sb.Append("[Information]\n");
            sb.Append("Stage=0\n");
            sb.Append("Lane=0\n");
            sb.Append("Serial=0\n");
            sb.Append("SerialStatus=0\n");
            sb.Append("Code=\"\"\n");
            sb.Append("BcrStatus=0\n");
            sb.Append("ProductID=\"\"\n");
            sb.Append("Rev=0\n");
            sb.Append("PlanID=\"\"\n");
            sb.Append("Output=46\n");
            sb.Append("LotName=\"\"\n");
            sb.Append("LotNumber=0\n");

            sb.Append("\n");

            sb.Append("[EquipmentInfo]\n");
            sb.Append("EQType=22\n");
            sb.Append("Vendor=\"na\"\n");
            sb.Append("ConverterVersion=\"\"\n");

            sb.Append("\n");

            sb.Append("[NozzleInspectionUnit]\n");
            sb.Append("UnitSerialNumber=\"na\"\n");
            sb.Append($"NozzleSerialNumber=\"333\"\n");
            sb.Append($"NozzleType=\"222\"\n");
            sb.Append("NozzleStatus=0\n");              //吸嘴检查结果
            sb.Append($"DATE=\"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}\"\n");
            sb.Append("UnitSystemVersion=\"\"\n");
            sb.Append("UnitUsageTime=\"\"\n");

            sb.Append("\n");

            sb.Append("[NozzleVacuum]\n");
            sb.Append("AirLeakStatus AirStuffStatus AirLeakValue AirLeakVThreshold AirStuffValue AirStuffThreshold\n");
            sb.Append("2 ");                         //流量结果
            sb.Append("2 ");     //负压结果
            sb.Append("0.0 ");                                        //流量实际值
            sb.Append("0.0 ");                                 //流量设定阈值
            sb.Append("0.0 ");                    //负压实际值
            sb.Append("0.0\n");           //负压设定阈值

            File.WriteAllText(fileName, sb.ToString());
        }
        private string MakeFileName(int nmu)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(DateTime.Now.ToString("yyyyMMddHHmmssfff"));   //日期
            sb.Append("+-+");
            sb.Append("99");                                            //顺序
            sb.Append("+-+");
            sb.Append("0");                                             //编号1
            sb.Append("+-+");
            sb.Append("0");                                             //编号2
            sb.Append("+-+");
            sb.Append("22");                                            //类别
            sb.Append("+-+");
            sb.Append("aaaaaaaaaaa");//条码
            sb.Append("+-+");
            sb.Append(nmu);                                             //清洗/检查
            sb.Append("+-+");
            sb.Append("");                                              //编号3
            sb.Append("+-+");
            sb.Append("");                                              //编号4
            sb.Append(".u20");
            return sb.ToString();
        }
        private async Task Data(Test item)
        {
            await Task.Delay(10);
            var result = Tests.First(p => p.Id == item.Id);
            result.Name = item.Name;
        }
    }
}
